package com.yy.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.entity.Payment;
import com.yy.service.Impl.PaymentServiceImpl;
import com.yy.utils.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 支付模块数据表(Payment)表控制层
 *
 * @author makejava
 * @since 2022-12-16 15:24:59
 */
@RestController
@RequestMapping("payment")
public class PaymentController {
    /**
     * 服务对象
     */
    @Resource
    private PaymentServiceImpl paymentService;
    @Value("${server.port}")
    private String port;
    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param payment 查询实体
     * @return 所有数据
     */
    @GetMapping("page")
    public Result<Page<Payment>> selectAll(Page<Payment> page, Payment payment) {
        return Result.ok(this.paymentService.page(page, new QueryWrapper<>(payment)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result<Payment> selectOne(@PathVariable Serializable id) {
        return Result.ok(this.paymentService.getById(id)).message("调用的eureka服务端口号为"+port);
    }

    /**
     * 新增数据
     *
     * @param payment 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    public Result<Boolean> insert(@RequestBody Payment payment) {
        return Result.ok(this.paymentService.save(payment)).message("调用的eureka服务端口号为"+port);
    }

    /**
     * 修改数据
     *
     * @param payment 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public Result<Boolean> update(@RequestBody Payment payment) {
        return Result.ok(this.paymentService.updateById(payment));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("delete")
    public Result<Boolean> delete(@RequestParam("idList") List<Long> idList) {
        return Result.ok(this.paymentService.removeByIds(idList));
    }
    @GetMapping("/client/lb")
    public String getPortLb(){
        return port;
    }

    @GetMapping("/timeout")
    public String timeout(){
        try{
            TimeUnit.SECONDS.sleep(6);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        return "调用端口号为"+port;
    }
}

